package view;

import domain.ATM;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.stage.WindowEvent;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class ATMViewController implements ControlledStage, Initializable {

    StageController myController;
    ATM atm;
    @FXML
    private Button balance;
    @FXML
    private Button deposit;
    @FXML
    private Button withdrawal;
    @FXML
    private Button refundCard;
    @FXML
    private TextField screen;
    @FXML
    private TextField balanceField;


    public void setStageController(StageController stageController) {
        this.myController = stageController;
        showMessage();
    }

    public void initialize(URL location, ResourceBundle resources) {
    }


    private void showMessage() {
        atm = myController.getAtm();
        screen.setText(atm.getCurAccount().getCustomer().getName() + " \u7528\u6237\u4f60\u597d! \u8bf7\u9009\u62e9\u670d\u52a1\u9879\u76ee\u3002");// XX用户你好! 请选择服务项目。


        myController.setAtm(atm);
    }


    public void goToMain(){
        myController.setStage(MainApp.mainViewID);
    }


    @FXML
    private void balanceAction(ActionEvent event) throws IOException {
//        atm = myController.getAtm();
        System.out.println("You clicked the Button " + balance.getId());
//        screen.setText(atm.getCurAccount().getBalance().getBalance());

        myController.loadStage(MainApp.balanceViewID, MainApp.balanceViewRes);
        myController.setStage(MainApp.balanceViewID);

    }
    @FXML
    private void depositAction(ActionEvent event) throws IOException {
        atm = myController.getAtm();
        System.out.println("You clicked the Button " + deposit.getId());
        atm.makeNewTransaction(deposit.getText());

        myController.setAtm(atm);
        myController.loadStage(MainApp.depositViewID, MainApp.depositViewRes);
        myController.setStage(MainApp.depositViewID,MainApp.mainViewID);

    }
    @FXML
    private void withdrawalAction(ActionEvent event) throws IOException {
        atm = myController.getAtm();
        System.out.println("You clicked the Button " + withdrawal.getId());

        atm.makeNewTransaction(withdrawal.getText());

        myController.setAtm(atm);
        myController.loadStage(MainApp.withdrawalViewID, MainApp.withdrawalViewRes);
        myController.setStage(MainApp.withdrawalViewID,MainApp.mainViewID);

    }
    @FXML
    private void refundCardAction(ActionEvent event) throws IOException {
        atm = myController.getAtm();
        System.out.println("You clicked the Button " + refundCard.getId());


        atm.refundCard();
        atm.setCurMessageType("5");
        myController.setAtm(atm);
        myController.setStage(MainApp.cardReaderViewID,MainApp.mainViewID);

        myController.loadStage(MainApp.messageViewID,MainApp.messageViewRes);
        myController.setStage(MainApp.messageViewID);

    }
}
